package com.calvin.tingapp;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;
import android.widget.Toast;
import com.calvin.android.annotation.Notice;
import com.calvin.android.json.JSONObject;
import com.calvin.android.notification.NotificationCenter;
import com.calvin.tingapp.config.AppConfig;
import com.calvin.tingapp.config.LrtsConfig;
import com.calvin.tingapp.config.NTConfig;
import com.calvin.tingapp.service.SerializationCenter;

public class MediaPlayService extends Service implements OnPreparedListener, MediaPlayer.OnCompletionListener {
    JSONObject mBookInfo;
    MediaPlayer mMediaPlayer;
    Integer mCurrentIndex;

    public MediaPlayService() {
        NotificationCenter.shared().scanNotice(this);
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnPreparedListener(this);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Notice(NTConfig.NT_MD_PLAY)
    public void onNtPlay(Object... argv) {
        mBookInfo = (JSONObject) argv[0];
        mCurrentIndex = (int) argv[1];
        boolean isLocal = argv.length >= 3 ? (Boolean) argv[2] : false;
        String url;
        if (isLocal) {
            String path = Environment.getExternalStoragePublicDirectory(AppConfig.SD_BOOK).getPath();
            String name = mBookInfo.getStringValue(LrtsConfig.K_NAME);
            url = String.format("%s/%s/%s.mp3", path, name, mCurrentIndex);
        } else {
            url = mBookInfo.getJSONObject(LrtsConfig.K_CHAPTER_LIST).getStringValue(mCurrentIndex.toString());
        }
        System.out.println(url);
        try {
            mMediaPlayer.reset();
            mMediaPlayer.setDataSource(this, Uri.parse(url));
            mMediaPlayer.prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
        }
        SerializationCenter.shared().addRecordBookList(mBookInfo);
        NotificationCenter.shared().postNotice(NTConfig.NT_CHANGE_MEDIA_INFO, 0, mBookInfo, mCurrentIndex);
    }

    @Notice(NTConfig.NT_MD_PAUSE)
    public void onNtPause(Object... argv) {
        if (mBookInfo != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.pause();
            NotificationCenter.shared().postNotice(NTConfig.NT_CHANGE_MEDIA_INFO, 2);
        } else {
            Toast.makeText(this, R.string.s_choice_book, Toast.LENGTH_SHORT).show();
        }
    }

    @Notice(NTConfig.NT_MD_RESUME)
    public void onNtResume(Object... argv) {
        if (mBookInfo != null && !mMediaPlayer.isPlaying()) {
            mMediaPlayer.start();
            NotificationCenter.shared().postNotice(NTConfig.NT_CHANGE_MEDIA_INFO, 1);
        } else {
            Toast.makeText(this, R.string.s_choice_book, Toast.LENGTH_SHORT).show();
        }
    }

    @Notice(NTConfig.NT_MD_NEXT)
    public void onNtNext(Object... argv) {
        if (mBookInfo != null) {
            mCurrentIndex++;
            String url = mBookInfo.getJSONObject(LrtsConfig.K_CHAPTER_LIST).getStringValue(mCurrentIndex.toString());
            try {
                mMediaPlayer.reset();
                mMediaPlayer.setDataSource(this, Uri.parse(url));
                mMediaPlayer.prepareAsync();
            } catch (Exception e) {
                e.printStackTrace();
            }
            NotificationCenter.shared().postNotice(NTConfig.NT_CHANGE_MEDIA_INFO, 0, mBookInfo, mCurrentIndex);
        } else {
            Toast.makeText(this, R.string.s_choice_book, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        NotificationCenter.shared().postNotice(NTConfig.NT_MD_NEXT);
    }
}
